﻿// Inner Fire 游戏引擎库
// UI - 组件控件
//
// Copyright (c) 2025 by 尔西大帝. All rights reserved.
//
// 用于UI的组件-基础类型
// 所有的UI控件都需要继承此类
//
// Author: 尔西大帝
// Email: 2670613871@qq.com
// Created: 2025-01-07

#include "ui_component.h"
#include "button.h"

namespace ifire::ui {

UIComponent::UIComponent(ikit::sstr name)
    : name_(name)
    , is_hover_(false)
    , visible_(true)
    , is_pushed_(false) {}

void UIComponent::SetHover(bool state) {
  if (is_hover_ != state) {
    is_hover_ = state;
    if (hover_callback_) {
      hover_callback_(this, state);
    }
  }
}

void UIComponent::SetPushed(bool state) {
  if (is_pushed_ != state) {
    is_pushed_ = state;
    if (pushed_callback_) {
      pushed_callback_(this, state);
    }
  }
}

void UIComponent::AddHoverChangedListener(HoverCallback callback) {
  hover_callback_ = callback;
}

void UIComponent::AddClickListener(ClickCallback callback) {
  click_callback_ = callback;
}

void UIComponent::AddPushedListener(PushedCallback callback) {
  pushed_callback_ = callback;
}

void DefaultHoverAction(ui::UIComponent* c, bool hover_state) {
  Button* btn = (ui::Button*)c;
  btn->ButtonImage()->SetVisible(hover_state);
  if (btn->Text()){
    btn->Text()->SetVisible(hover_state);
  }  
};

void DefaultPushedAction(ui::UIComponent* obj, bool state) {
  if (state) {
    obj->SetScale({0.9f, 0.9f});
  } else {
    obj->SetScale({1.0f, 1.0f});
  }
};

void UIComponent::SetDefaultHoverAction() {
  AddHoverChangedListener(DefaultHoverAction);
}

void UIComponent::SetDefaultPushedAction() {
  AddPushedListener(DefaultPushedAction);
}

void UIComponent::PerformClick() {
  if (click_callback_) {
    click_callback_(this);
  }
}

void UIComponent::SetVisible(bool state) { visible_ = state; }

void UIComponent::SetScale(XMFLOAT2 scale) {}

void UIComponent::SetRefresh() {}

} // namespace ifire::ui